package com.edusite
import com.edusite.Personas

class ContactosController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [contactosInstanceList: Contactos.list(params), contactosInstanceTotal: Contactos.count()]
    }
    def listAlum = {
    	def contactosInstanceList = Contactos.findAllByUsuario(session.user.usuario)
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [contactosInstanceList: contactosInstanceList, contactosInstanceTotal: Contactos.count()]
    }
    def listProfe = {
    	def contactosInstanceList = Contactos.findAllByUsuario(session.user.usuario)
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [contactosInstanceList: contactosInstanceList, contactosInstanceTotal: Contactos.count()]
    }
    def create = {
        def contactosInstance = new Contactos()
        contactosInstance.properties = params
        return [contactosInstance: contactosInstance]
    }
    def createProfe = {
        def contactosInstance = new Contactos()
        contactosInstance.properties = params
        return [contactosInstance: contactosInstance]
    }
    def createAlum = {
        def contactosInstance = new Contactos()
        contactosInstance.properties = params
        return [contactosInstance: contactosInstance]
    }

    def save = {
        def contactosInstance = new Contactos(params)
        println params
        if (contactosInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'contactos.label', default: 'Contactos'), contactosInstance.id])}"
            redirect(action: "show", id: contactosInstance.id)
        }
        else {
            render(view: "create", model: [contactosInstance: contactosInstance])
        }
    }
    
    def saveAlum = {
/*   	def personaInstance = Personas.findByUsuario()
        println params
        def mipersona = Personas.findByUsuario(params.ncontacto)
        params.contacto = mipersona
        params.contacto.id= mipersona.id
        println params */

        def contactosInstance = new Contactos(
            usuario: session.user.usuario,
            contacto:Personas.findByUsuario(params.ncontacto),
            )
        
//        println "contactosInstance" + contactosInstance
        if (contactosInstance.save()) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'contactos.label', default: 'Contactos'), contactosInstance.id])}"
            redirect(action: "listAlum")
        }
        else {
            render(view: "createAlum", model: [contactosInstance: contactosInstance])
        }
    }
    def saveProfe = {
/*   	def personaInstance = Personas.findByUsuario()
        println params
        def mipersona = Personas.findByUsuario(params.ncontacto)
        params.contacto = mipersona
        params.contacto.id= mipersona.id
        println params */

        def contactosInstance = new Contactos(
            usuario: session.user.usuario,
            contacto:Personas.findByUsuario(params.ncontacto),
            )
        
//        println "contactosInstance" + contactosInstance
        if (contactosInstance.save()) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'contactos.label', default: 'Contactos'), contactosInstance.id])}"
            redirect(action: "listProfe")
        }
        else {
            render(view: "createProfe", model: [contactosInstance: contactosInstance])
        }
    }

    def show = {
        def contactosInstance = Contactos.get(params.id)
        if (!contactosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'contactos.label', default: 'Contactos'), params.id])}"
            redirect(action: "list")
        }
        else {
            [contactosInstance: contactosInstance]
        }
    }
    def showAlum = {
        def contactosInstance = Contactos.get(params.id)
        if (!contactosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'contactos.label', default: 'Contactos'), params.id])}"
            redirect(action: "list")
        }
        else {
            [contactosInstance: contactosInstance]
        }
    }
    def showProfe = {
        def contactosInstance = Contactos.get(params.id)
        if (!contactosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'contactos.label', default: 'Contactos'), params.id])}"
            redirect(action: "list")
        }
        else {
            [contactosInstance: contactosInstance]
        }
    }


    def edit = {
        def contactosInstance = Contactos.get(params.id)
        if (!contactosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'contactos.label', default: 'Contactos'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [contactosInstance: contactosInstance]
        }
    }

    def update = {
        def contactosInstance = Contactos.get(params.id)
        if (contactosInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (contactosInstance.version > version) {
                    
                    contactosInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'contactos.label', default: 'Contactos')] as Object[], "Another user has updated this Contactos while you were editing")
                    render(view: "edit", model: [contactosInstance: contactosInstance])
                    return
                }
            }
            contactosInstance.properties = params
            if (!contactosInstance.hasErrors() && contactosInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'contactos.label', default: 'Contactos'), contactosInstance.id])}"
                redirect(action: "show", id: contactosInstance.id)
            }
            else {
                render(view: "edit", model: [contactosInstance: contactosInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'contactos.label', default: 'Contactos'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def contactosInstance = Contactos.get(params.id)
        if (contactosInstance) {
            try {
                contactosInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'contactos.label', default: 'Contactos'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'contactos.label', default: 'Contactos'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'contactos.label', default: 'Contactos'), params.id])}"
            redirect(action: "list")
        }
    }
}
